package org.cmroot.certmaster.model;

import org.cmroot.certmaster.shared.dto.DTO;
import org.cmroot.certmaster.shared.dto.X509DTO;
import org.cmroot.certmaster.shared.dto.ext.X509ExtDTO;
import org.hibernate.annotations.Cascade;
import javax.persistence.*;
import java.math.BigInteger;
import java.util.Date;
import java.util.Set;

/**
 * Copyright (c) 2010 Andrey Domas <andrey.domas at gmail.com>.
 * *
 * This file is part of certmaster.
 * *
 * Certmaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * *
 * Certmaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 */

@Entity
@Table(name = "x509")
public class X509 extends PKCS {

    //signed request
    @ManyToOne()
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    @JoinColumn(name = "pkcs10_id", nullable = true)
    public PKCS10 getPKCS10() { return this.request; }
    public void setPKCS10(PKCS10 req) { this.request = req; }
    private PKCS10 request;

    //signed certs
    @OneToMany(mappedBy = "issuer", cascade = CascadeType.ALL)
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    public Set<X509> getCerts() { return this.certs; }
    public void setCerts(Set<X509> certs) { this.certs = certs; }
    private Set<X509> certs;

    @ManyToOne(cascade = CascadeType.ALL)
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    @JoinColumn(name = "issuer_id", nullable = true)
    public X509 getIssuer() { return this.issuer; }
    public void setIssuer(X509 issuer) { this.issuer = issuer; }
    private X509 issuer;

    @Column(nullable = false)
    public boolean isSelfSigned() { return selfSigned; }
    public void setSelfSigned(boolean selfSigned) { this.selfSigned = selfSigned; }
    private boolean selfSigned;

    @Column(nullable = false)
    public boolean isCA() { return CA; }
    public void setCA(boolean CA) { this.CA = CA; }
    private boolean CA;

    @Column(nullable = false)
    public Date getNotBefore() { return this.notBefore; }
    public void setNotBefore(Date notBefore) { this.notBefore = notBefore; }
    private Date notBefore;

    @Column(nullable = false)
    public Date getNotAfter() { return this.notAfter; }
    public void setNotAfter(Date notAfter) { this.notAfter = notAfter; }
    private Date notAfter;

    @Column(nullable = false)
    public BigInteger getSerial() { return serial; }
    public void setSerial(BigInteger serial) { this.serial = serial; }
    private BigInteger serial;

    @Override
    public X509DTO createDTO() {
        X509DTO dto = new X509DTO();
        dto.setId(getId());
        dto.setCN(getCN());
        dto.setChildCount(this.getCerts() == null ? 0 : this.getCerts().size());
        return dto;
    }

    @Override
    public X509ExtDTO createExtDTO() {
        X509ExtDTO ext_dto = new X509ExtDTO();
        ext_dto.setId(getId());
        ext_dto.setCN(getCN());
        ext_dto.setNotBefore(getNotBefore());
        ext_dto.setNotAfter(getNotAfter());
        ext_dto.setSerial(getSerial());
        ext_dto.setChildCount(this.getCerts() == null ? 0 : this.getCerts().size());
        return ext_dto;
    }
}
